package DatagramSocket2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Server {
    public static void main(String[] args) throws Exception {
        System.out.println("服务端启动^^");
        // 1.创建DatagramSocket对象（服务端对象）
        DatagramSocket socket = new DatagramSocket(6666);
        // 2.创建DatagramPacket对象接受数据（数据包对象）
        byte[] buffer = new byte[1024 * 64];
        DatagramPacket packet = new DatagramPacket(buffer,buffer.length);
        // 3.使用DatagramSocket对象的receive方法，传入DatagramPacket对象
        while(true){
            socket.receive(packet);
            int len = packet.getLength();
            String rs = new String(buffer,0,len);
            System.out.println(rs);
            System.out.println(packet.getAddress().getHostAddress());
            System.out.println(packet.getPort());
            System.out.println("--------------------");
        }
    }
}
